package com.uenpay.util.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.uenpay.util.R;

/**
 * Created by PS on 2016/12/20.
 */

public class UenEditTextView extends LinearLayout {

    /**
     * Whether or not to show the underline. Shown by default
     */
    private boolean hideUnderline;

    /**
     * the spacing between the main text and the bottom components (bottom
     * ellipsis, helper/error text, characters counter).
     */
    private int bottomSpacing;

    /**
     * Underline's color
     */
    private int underlineColor = getResources().getColor(R.color.uenedittextview_underline);

    /**
     * Left Icon
     */
    private Bitmap iconLeftBitmaps;
    private int leftBitmapRightMagin = getPixel(5);
    private int leftBitmapLeftMagin = getPixel(5);

    /**
     * Right Icon
     */
    private Bitmap iconRightBitmaps;
    private int rightBitmapRightMagin = getPixel(5);
    private int rightBitmapLeftMagin = getPixel(5);

    /**
     * editText
     */
    private EditText mChildView = null;
    private ViewGroup.LayoutParams mParams = null;
    private int textMaginTop = getPixel(5);
    private int textMaginBottom = getPixel(5);
    private int textMaginRight = getPixel(5);
    private int textMaginLeft = getPixel(5);

    private int realTextLeftMagin = 0;
    private int realTextRightMagin = 0;

    private float rightStartX = 0;
    private float rightEndX = 0;
    private float rightStartY = 0;
    private float rightEndY = 0;

    private RightIconListener rightIconListener;

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    public UenEditTextView(Context context) {
        super(context);
        init(context, null);
    }

    public UenEditTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public UenEditTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        setWillNotDraw(false);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.UenEditTextView);
        textMaginTop = typedArray.getDimensionPixelSize(R.styleable.UenEditTextView_textTopMargin, getPixel(5));
        textMaginBottom = typedArray.getDimensionPixelSize(R.styleable.UenEditTextView_textBottomMargin, getPixel(5));
        textMaginLeft = typedArray.getDimensionPixelSize(R.styleable.UenEditTextView_textLeftMargin, getPixel(5));
        textMaginRight = typedArray.getDimensionPixelSize(R.styleable.UenEditTextView_textRightMargin, getPixel(5));

        leftBitmapLeftMagin = typedArray.getDimensionPixelSize(R.styleable.UenEditTextView_leftImageLeftMargin, getPixel(5));
        leftBitmapRightMagin = typedArray.getDimensionPixelSize(R.styleable.UenEditTextView_leftImageRightMargin, getPixel(5));
        rightBitmapLeftMagin = typedArray.getDimensionPixelSize(R.styleable.UenEditTextView_rightImageLeftMargin, getPixel(5));
        rightBitmapRightMagin = typedArray.getDimensionPixelSize(R.styleable.UenEditTextView_rightImageRightMargin, getPixel(5));

        underlineColor = typedArray.getColor(R.styleable.UenEditTextView_lineColor, getResources().getColor(R.color.uenedittextview_underline));
        setHideUnderline(typedArray.getBoolean(R.styleable.UenEditTextView_hideUnderLine, false));

        int leftIconId = typedArray.getResourceId(R.styleable.UenEditTextView_leftIcon, 0);
        int rightIconId = typedArray.getResourceId(R.styleable.UenEditTextView_rightIcon, 0);
        if (leftIconId != 0) {
            setIconLeftBitmaps(leftIconId);
        }
        if (rightIconId != 0) {
            setIconRightBitmaps(rightIconId);
        }
        typedArray.recycle();
    }

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        if (child instanceof EditText) {
            mChildView = (EditText) child;
            mParams = params;
            if (mChildView.getText() != null) {
                mChildView.setSelection(mChildView.getText().length());
            }
            super.addView(child, index, getEditTextParams());
        } else {
            throw new IllegalArgumentException("can not support!");
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int lineStartY = getHeight();

        // draw the icon(s)
        paint.setAlpha(255);
        if (iconLeftBitmaps != null) {
            Bitmap icon = iconLeftBitmaps;
            int iconTop = (getHeight() - icon.getHeight()) / 2;
            canvas.drawBitmap(icon, leftBitmapLeftMagin, iconTop, paint);
        }
        if (iconRightBitmaps != null) {
            Bitmap icon = iconRightBitmaps;
            int iconTop = (getHeight() - icon.getHeight()) / 2;
            rightStartX = getX() + getWidth() - icon.getWidth() - 2 * rightBitmapLeftMagin - rightBitmapRightMagin;
            rightEndX = rightStartX + icon.getWidth() + 2 * rightBitmapRightMagin;
            rightStartY = getY() + iconTop;
            rightEndY = rightStartY + icon.getHeight();
            canvas.drawBitmap(icon, getWidth() - icon.getWidth() - rightBitmapLeftMagin - rightBitmapRightMagin
                    , iconTop, paint);
        }

        // draw the underline
        if (!hideUnderline) {
            paint.setColor(underlineColor);
            canvas.drawRect(0, lineStartY - getPixel(1) / 2, getWidth(), lineStartY, paint);
        }
    }

    private int getPixel(int dp) {
        return dp2px(dp);
    }

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
    }

    public Bitmap getIconRightBitmaps() {
        return iconRightBitmaps;
    }

    public void setIconRightBitmaps(int iconRightBitmaps) {
        this.iconRightBitmaps = BitmapFactory.decodeResource(getResources(), iconRightBitmaps);
        removeView(mChildView);

        realTextRightMagin = this.iconRightBitmaps.getWidth() + rightBitmapLeftMagin
                + rightBitmapRightMagin + textMaginRight;
        if (mChildView != null) {
            addView(mChildView, 0, getEditTextParams());
        }
    }

    public Bitmap getIconLeftBitmaps() {
        return iconLeftBitmaps;
    }

    public void setIconLeftBitmaps(int iconLeftBitmaps) {
        this.iconLeftBitmaps = BitmapFactory.decodeResource(getResources(), iconLeftBitmaps);
        removeView(mChildView);

        realTextLeftMagin = this.iconLeftBitmaps.getWidth() + leftBitmapRightMagin
                + leftBitmapLeftMagin + textMaginLeft;
        if (mChildView != null) {
            addView(mChildView, 0, getEditTextParams());
        }
    }

    public LayoutParams getEditTextParams() {
        LayoutParams newLp = new LayoutParams(mParams);
        newLp.leftMargin = realTextLeftMagin;
        newLp.rightMargin = realTextRightMagin;
        newLp.topMargin = textMaginTop;
        newLp.bottomMargin = textMaginBottom;
        return newLp;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN && iconRightBitmaps != null) {
            insideRightButton(event);
        }
        return super.onTouchEvent(event);
    }

    private void insideRightButton(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        //如果x&y坐标在图片坐标内 触发点击事件 算上margin的距离
        if (rightStartX <= x && x <= rightEndX && y <= rightEndY && rightStartY <= y) {
            if (rightIconListener != null) {
                rightIconListener.onClick();
            }
        }
    }

    public void setRightIconListener(RightIconListener listener) {
        rightIconListener = listener;
    }

    public interface RightIconListener {
        void onClick();
    }

    public int getUnderlineColor() {
        return underlineColor;
    }

    public void setUnderlineColor(int underlineColor) {
        this.underlineColor = underlineColor;
    }

    public int getBottomSpacing() {
        return bottomSpacing;
    }

    public void setBottomSpacing(int bottomSpacing) {
        this.bottomSpacing = bottomSpacing;
    }

    public boolean isHideUnderline() {
        return hideUnderline;
    }

    public void setHideUnderline(boolean hideUnderline) {
        this.hideUnderline = hideUnderline;
    }

}
